﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;

namespace CommonUtil
{
    /// <summary>
    /// Socket通信通用信息交流类
    /// </summary>
    public class SocketCommunication
    {

        public void CloseSocket(TcpClient client)
        {
            if (client!=null)
            {
                client.Close();
            }
        }

        public string ReadNetStreamDefault(TcpClient client, NetworkStream nstream)
        {
            string receiveMsg = string.Empty;
            byte[] receiveBytes = new byte[client.ReceiveBufferSize];
            int byteOfRead = 0;
            if (nstream.CanRead)
            {
                do
                {
                    byteOfRead = nstream.Read(receiveBytes, 0, receiveBytes.Length);
                    receiveMsg = Encoding.Default.GetString(receiveBytes, 0, byteOfRead);
                }
                while (nstream.DataAvailable);
            }
            return receiveMsg;
        }

        public void WriteNetStreamDefault(TcpClient client, NetworkStream nstream, string sendMsg)
        {
            if (nstream.CanWrite)
            {
                byte[] msgByte = Encoding.Default.GetBytes(sendMsg);
                nstream.Write(msgByte, 0, msgByte.Length);
            }
        }

        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="client"></param>
        public void SendMsg(string msg, TcpClient client)
        {
            NetworkStream ns = client.GetStream();
            if (ns.CanWrite)
            {
                byte[] msgByte = Encoding.Default.GetBytes(msg);
                ns.Write(msgByte, 0, msgByte.Length);
            }
        }

        /// <summary>
        /// 接收消息
        /// </summary>
        /// <param name="client"></param>
        /// <returns></returns>
        public string ReceiveMsg(TcpClient client)
        {
            string receiveMsg = string.Empty;
            byte[] receiveBytes = new byte[client.ReceiveBufferSize];
            int byteRead = 0;
            NetworkStream ns = client.GetStream();
            if (ns.CanRead)
            {
                do
                {
                    byteRead = ns.Read(receiveBytes, 0, receiveBytes.Length);
                    receiveMsg = Encoding.Default.GetString(receiveBytes, 0, byteRead);
                }
                while (ns.DataAvailable);
            }
            return receiveMsg;
        }

        #region 文件收发
        /// <summary>
        /// 
        /// </summary>
        /// <param name="client">TcpClient != null</param>
        /// <param name="nstream">NetworkStream != null</param>
        /// <param name="filePath">文件必须存在。AppDomain.CurrentDomain.BaseDirectory + fileName.ext</param>
        public string SendFileStream(TcpClient client, NetworkStream nstream, string filePath,int byteSize)
        {
            FileInfo fileInfo = new FileInfo(filePath);            
            FileStream fstream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            if (byteSize < 4096) byteSize = 4096;
            byte[] fileBuffer = new byte[byteSize];
            int byteOfRead = 0;
            int totalBytes = 0;

            do
            {
                System.Threading.Thread.Sleep(10);
                byteOfRead = fstream.Read(fileBuffer, 0, fileBuffer.Length);
                nstream.Write(fileBuffer, 0, byteSize);
                totalBytes += byteOfRead;   //发送了的字节数
            }
            while (byteOfRead > 0);
            return totalBytes.ToString();
        }

        public string ReceiveFileStream(TcpClient client, NetworkStream nstream)
        {
            return null;
        }
        #endregion 
    }
}
